package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.InvoiceInfoBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.InvoiceInfoMapper;
import com.molichuxing.services.infrastructure.dto.request.create.InvoiceInfoCreateDto;
import com.molichuxing.services.infrastructure.dto.response.InvoiceInfoDto;
import com.molichuxing.services.infrastructure.service.InvoiceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 发票申请 服务实现类
 * </p>
 *
 * @author zhaopenghui
 * @since 2019-09-11
 */
@Service("invoiceInfoService")
public class InvoiceInfoServiceImpl implements InvoiceInfoService {
    @Autowired
    InvoiceInfoMapper invoiceInfoMapper;

    /**
     * 获取发票列表
     *
     * @param map      key:status Integer 发票状态
     *                 key:invoiceApplyIdList List 发票申请id
     *                 key：businessType Integer 业务类型
     *                 key:startApplyTime LocalDateTime 申请开始时间
     *                 key:endApplyTime LocalDateTime 申请结束时间
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public Paged<InvoiceInfoDto> getInvoiceInfoPage(Map<String, Object> map, Integer pageNum, Integer pageSize) throws Exception {
        long pageCount = invoiceInfoMapper.getPageCount(map);
        if (pageCount < 1) {
            return null;
        }

        List<InvoiceInfoBean> invoiceInfoBeanList = invoiceInfoMapper.getPage(map, pageNum, pageSize);
        List<InvoiceInfoDto> invoiceInfoDtoList = Convert.toInvoiceInfoDtoList(invoiceInfoBeanList);
        return new Paged<InvoiceInfoDto>(invoiceInfoDtoList, pageCount, pageSize);
    }

    /**
     * 根据状态获取发票数量
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map<Integer, Integer> getInvoiceCountByStatus() throws Exception {
        List<TabDto> statusList = invoiceInfoMapper.getInvoiceCountByStatus();
        if (null == statusList || statusList.size() == 0) {
            return null;
        }

        Map<Integer, Integer> returnMap = new HashMap<Integer, Integer>();
        for (TabDto tabDto : statusList) {
            Integer type = tabDto.getType();
            Integer count = tabDto.getCount();
            returnMap.put(type, count);
        }

        return returnMap;
    }

    /**
     * 修改发票状态
     *
     * @param invoiceInfoId
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyInvoiceStatus(Integer invoiceInfoId, Integer status) throws Exception {
        if (null == invoiceInfoId) {
            throw new BizException("发票不存在");
        }

        InvoiceInfoBean invoiceInfoBean = invoiceInfoMapper.getById(invoiceInfoId);
        if (null == invoiceInfoBean) {
            throw new BizException("发票不存在");
        }

        int result = invoiceInfoMapper.modifyStatusById(invoiceInfoId, status);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 发票申请批量添加
     *
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public Boolean createInvoiceInfoBatch(List<InvoiceInfoCreateDto> list) throws Exception {
        if (null == list || list.size() < 1) {
            return false;
        }

        int result = invoiceInfoMapper.createInvoiceInfoBatch(list);
        if (result == list.size()) {
            return true;
        } else {
            return false;
        }
    }


}
